﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Publicuse.Entity.AttributeUtil.Validate
{
    /// <summary>
    /// 字段值合法性验证
    /// </summary>
    public static class DataValidateExtend
    {
        /// <summary>
        /// 字段值合法性验证
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool ValidateModel<T>(this T t, out string msg)
        {
            msg = string.Empty;
            Type type = typeof(T);
            StringBuilder falsemsg = new StringBuilder();
            foreach (var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(BaseValidateAttribute), true))
                {
                    string remark = string.Empty;
                    object oValue = prop.GetValue(t);
                    var attributeList = prop.GetCustomAttributes<BaseValidateAttribute>();
                    if (prop.IsDefined(typeof(RemarkAttribute), true))
                    {
                        var reamrkattribute = prop.GetCustomAttribute<RemarkAttribute>();
                        remark = reamrkattribute.GetName;
                    }
                    else
                    {
                        remark = prop.Name;
                    }
                    foreach (var attribute in attributeList)
                    {
                        if (!attribute.Validate(oValue))
                        {
                            switch (attribute.GetType().Name)
                            {
                                case "LengthAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<LengthAttribute>();
                                        remark += $"长度不合法：{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "RequiredAttribute":
                                    falsemsg.Append("不能为空");
                                    break;
                                case "IntValuesAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<IntValuesAttribute>();
                                        remark += $"不合法：{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "FormatAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<FormatAttribute>();
                                        remark += $"格式不正确：{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "MinMaxAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<MinMaxAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "CardNoAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<CardNoAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "MobileAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<MobileAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "NotequaltoAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<NotequaltoAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "NumberrangeAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<NumberrangeAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "DomainAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<DomainAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "DateAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<DateAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "QQAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<QQAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "PostalCodeAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<PostalCodeAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "IPAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<IPAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "EmailAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<EmailAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "PhoneAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<PhoneAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "ChineseAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<ChineseAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "EnAllAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<EnAllAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "PhoneMobileAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<PhoneMobileAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "MaxAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<MaxAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                                case "NotetovAttribute":
                                    {
                                        var arf = prop.GetCustomAttribute<NotetovAttribute>();
                                        remark += $"{arf.GetRemark}；当前信息为：{oValue}-{prop.Name}|";
                                    }
                                    break;
                            }

                            falsemsg.Append(remark+"<br/>");
                        }
                        else
                        {
                            switch (attribute.GetType().Name)
                            {
                                case "ChangeValueAttribute":
                                    {
                                        prop.SetValue(t, oValue + "66666");
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(falsemsg.ToString()))
            {
                msg = "数据校验失败请检查：<br/>" + falsemsg.ToString();
                return false;
            }
            return true;
        }
    }
}
